package cn.itcast.web.controller.cargo;

import cn.itcast.domain.cargo.Contract;
import cn.itcast.domain.cargo.ContractExample;
import cn.itcast.domain.system.User;
import cn.itcast.service.cargo.ContractService;
import cn.itcast.web.controller.BaseController;
import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

/**
 * 购销合同的web层（消费者）
 */
@Controller
@RequestMapping("/cargo/contract")
public class ContractController extends BaseController {

 @Reference
 private ContractService contractService;

 /**
  * 分页
  */
 @RequestMapping("/list")
 public String list(@RequestParam(defaultValue = "1") int page, @RequestParam(defaultValue = "5")int size) {
  //1.创建example
  ContractExample example = new ContractExample();
  //2.通过example创建criteria对象
  ContractExample.Criteria criteria = example.createCriteria();
  //3.向criteria对象设置条件
  criteria.andCompanyIdEqualTo(getLoginCompanyId());
  //4.发起调用
  //设置排序
  example.setOrderByClause("create_time DESC");

  /**
   * 细粒度权限控制
   */
  User user = getLoginUser();
  Integer degree = user.getDegree();
  if (degree == 4) {
   //4.普通员工     --查看自己签订的合同
   criteria.andCreateByEqualTo(user.getId());
  } else if (degree == 3) {
   //3.部门经理     --查看当前部门下的所有合同
   criteria.andCreateDeptEqualTo(user.getDeptId());
  } else if (degree == 2) {
   //2.管理本部门和下属部门
   criteria.andCreateDeptLike(user.getDeptId() + "%");
  }

  PageInfo info = contractService.findAll(page, size, example);
  request.setAttribute("page",info);
  //跳转页面
  return "cargo/contract/contract-list";
 }

 //进入到保存页面
 @RequestMapping("/toAdd")
 public String toAdd() {
  return "cargo/contract/contract-add";
 }

 //进入修改页面
 @RequestMapping("/toUpdate")
 public String toUpdate(String id) {
  //1.根据id查询
  Contract contract = contractService.findById(id);
  request.setAttribute("contract",contract);
  return "cargo/contract/contract-update";
 }


 //修改或者保存
 @RequestMapping("/edit")
 public String edit(Contract contract) {
  //1.设置企业属性
  contract.setCompanyId(getLoginCompanyId());
  contract.setCompanyName(getLoginCompanyName());
  //2.判断是否具有id
  if (StringUtils.isEmpty(contract.getId())) {
   //3.没有id，调用service保存
   //设置购销合同的创建人和创建人部门
   User user = getLoginUser();
   contract.setCreateBy(user.getId());//创建人
   contract.setCreateDept(user.getDeptId());//创建人部门
   contractService.save(contract);
  }else {
   //4.存在id，调用service更新
   contractService.update(contract);
  }
  //5.重定向到列表
  return  "redirect:/cargo/contract/list.do";
 }

 /**
  * 合同提交
  * /cargo/contract/submit.do?id=c189084f-7a37-4c01-b717-ce0658dbe78e
  */
 @RequestMapping("/submit")
 public String submit(String id) {
  Contract contract = contractService.findById(id);
  contract.setState(1);
  contractService.update(contract);
  return "redirect:/cargo/contract/list.do";
 }

 /**
  * 合同取消
  * /cargo/contract/cancel.do?id=c189084f-7a37-4c01-b717-ce0658dbe78e
  */
 @RequestMapping("/cancel")
 public String cancel(String id) {
  Contract contract = contractService.findById(id);
  contract.setState(0);
  contractService.update(contract);
  return "redirect:/cargo/contract/list.do";
 }

 /**
  * 根据id删除购销合同
  * /cargo/contract/delete.do?id=c189084f-7a37-4c01-b717-ce0658dbe78e
  */
 @RequestMapping("/delete")
 public String delete(String id) {
  contractService.delete(id);
  return "redirect:/cargo/contract/list.do";
 }
}